package com.Gientech.springbucks.resultTest;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;

import java.util.List;

public class testOne {
    public static void main(String[] args) {
        System.out.println("1111 = " + 1111);
//        String  sql = "SELECT * FROM abc limit2 1"; //错误的，会报错
        String sql = "SELECT * FROM abc limit 1";
        String formtStr = formatSql(sql);
        System.out.println("formtStr = " + formtStr);
    }

    public static boolean verifySql(String sql) {
        MySqlStatementParser
                parser = new MySqlStatementParser(sql);
        boolean flag = true;
        try {

            List<SQLStatement> smtList = parser.parseStatementList();
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }

    public static String formatSql(String sql) {
        //先校验sql
        boolean b = verifySql(sql);
        System.out.println("b = " + b);
        //格式化
        String formatSql = com.alibaba.druid.sql.SQLUtils.formatMySql(sql);
        //判断是否已经格式化过
        if (!sql.equals(formatSql)) {
            return formatSql;
        }
        return null;
    }
}
